package com.example.shoppingdemo.Center;

import android.util.AtomicFile;
import android.util.Log;

import com.example.shoppingdemo.BaseApplication;

import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;


public class CenterClass implements ProtuctClass {
    private static final String TAG = ProtuctClass.class.getSimpleName();
    @Override
    public void requestProductClass(CenterClass.OnClassoficationResultListener rl) {
            List<LeftModel>modelList=new ArrayList<>();
            BaseApplication.ishoppingRequest.requestProductClass()
                    .flatMap(new Func1<CenterProduct, Observable<CenterData>>() {

                        @Override
                        public Observable<CenterData> call(CenterProduct centerProduct) {
                            return Observable.from(centerProduct.getData());
                        }
                    }).map(new Func1<CenterData, LeftModel>() {
                @Override
                public LeftModel call(CenterData centerData) {
                    LeftModel model=new LeftModel();
                    List<RightModel>rightModels=new ArrayList<>();
                    model.setName(centerData.getName());
                    Log.d(TAG, "call: ........"+centerData.getName());
                    for (int i = 0; i < centerData.getChildren().length; i++) {
                        RightModel model1=new RightModel();
                        model1.setId(centerData.getChildren()[i].getId());
                        model1.setIcon(centerData.getChildren()[i].getIcon());
                        model1.setName(centerData.getChildren()[i].getName());
                        Log.d(TAG, "call: ............"+centerData.getChildren()[i].getIcon());
                        Log.d(TAG, "call: ............"+centerData.getChildren()[i].getId());
                        Log.d(TAG, "call: ............"+centerData.getChildren()[i].getName());
                        rightModels.add(model1);
                    }
                    model.setRightModels(rightModels);
                    return model;
                }
            }).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Subscriber<LeftModel>() {
                        @Override
                        public void onCompleted() {
                            if (null!=rl){
                                rl.onClassoficationResult(modelList);
                            }
                        }

                        @Override
                        public void onError(Throwable e) {

                        }

                        @Override
                        public void onNext(LeftModel leftModel) {
                            modelList.add(leftModel);
                        }
                    });
    }

    public interface OnClassoficationResultListener{
           void onClassoficationResult(List<LeftModel>modelList);
                }
}
